package com.epam.edu.task02.Strings;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import org.apache.log4j.Logger;

public class WorkWithFile {
	private static BufferedReader bufferedReader;
	private static Logger logger = Logger.getLogger(App.class);
	private static String outputFilePath = "src/main/resources/output.txt";
	private static String inputFilePath = "src/main/resources/input.txt";
	
	public static String readFromFile() {
		StringBuilder text = new StringBuilder();
		try {
			FileInputStream fstream = new FileInputStream(inputFilePath);
			DataInputStream inputStream = new DataInputStream(fstream);
			bufferedReader = new BufferedReader(new InputStreamReader(
					inputStream));
			logger.info("Read from file");
			String inputString;
			while ((inputString = bufferedReader.readLine()) != null) {
				text.append(inputString + " ");
			}
		} catch (IOException ex) {
			logger.error(ex.getMessage());
		}
		return text.toString();

	}
	
	public static void writeIntoFile(String text) {
		try {
			PrintWriter out = new PrintWriter(new OutputStreamWriter(
					new FileOutputStream(outputFilePath)), false);
			out.println(text);
		} catch (IOException exception) {
			logger.error(exception.getMessage());
		}
	}
}
